<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" :inline="true">
      <el-form-item label="用户昵称">
        <el-input
          @keyup.enter="handleQuery"
          v-model="queryParams.keyword"
          style="width: 200px"
          placeholder="请输入用户昵称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb15">
      <el-col :span="1.5">
        <el-button type="success" plain class="el-icon-plus" @click="openModel(undefined)">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain :disabled="roleIdList.length === 0"  class="el-icon-delete"
          @click="handleDelete(undefined)">批量删除</el-button>
      </el-col>
        <RightToolBar @changeShow="changeShowSearch" :showSearch="showSearch" @queryTable="getList"></RightToolBar>
    </el-row>
    <!-- 表格展示 -->
    <el-table border :data="userList" v-loading="loading">
      <!-- 用户头像 -->
      <el-table-column prop="avatar" label="头像" align="center" width="100">
        <template #default="scope">
          <img :src="scope.row.avatar" width="40" height="40" />
        </template>
      </el-table-column>
      <!-- 昵称 -->
      <el-table-column
        prop="nickname"
        label="昵称"
        align="center"
        width="140"
      ></el-table-column>
      <!-- 登录方式 -->
      <el-table-column
        prop="loginType"
        label="登录方式"
        align="center"
        width="100"
      >
        <template #default="scope">
          <el-tag type="success" v-if="scope.row.loginType == 1">邮箱</el-tag>
          <el-tag v-if="scope.row.loginType == 2">QQ</el-tag>
          <el-tag type="danger" v-if="scope.row.loginType == 3">Gitee</el-tag>
          <el-tag type="info" v-if="scope.row.loginType == 4">Github</el-tag>
        </template>
      </el-table-column>
      <!-- 用户角色 -->
      <el-table-column width="250" prop="roleList" label="用户角色" align="center">
        <template #default="scope">
          <el-tag
            v-for="item in scope.row.roleList"
            :key="item.id"
            style="margin-right: 4px; margin-top: 4px"
          >
            {{ item.roleName }}
          </el-tag>
        </template>
      </el-table-column>
      <!-- 状态 -->
      <el-table-column prop="status" label="状态" align="center" width="100">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isDisable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
            @change="handleChangeStatus(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!-- 登录ip -->
      <el-table-column
        prop="ipAddress"
        label="登录ip"
        align="center"
        width="140"
      ></el-table-column>
      <!-- 登录地址 -->
      <el-table-column
        prop="ipSource"
        label="登录地址"
        align="center"
        width="140"
      ></el-table-column>
      <!-- 创建时间 -->
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        width="130"
      >
        <template #default="scope">
          <div class="create-time">
            <el-icon>
              <clock />
            </el-icon>
            <span style="margin-left: 10px">{{
              formattedDate(scope.row.createTime)
            }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- 登录时间 -->
      <el-table-column
        prop="loginTime"
        label="登录时间"
        align="center"
        width="130"
      >
        <template #default="scope">
          <div class="create-time">
            <el-icon>
              <clock />
            </el-icon>
            <span style="margin-left: 10px">{{
              formattedDate(scope.row.loginTime)
            }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
             type="warning"
            class="el-icon-edit"
            size="mini"
            plain
            icon="Edit"
            link
            @click="openModel(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-if="count > 0"
      :total="count"
      :page="queryParams.current"
      :limit="queryParams.size"
      @pagination="getList"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog title="修改用户" :visible.sync="update" width="500px" append-to-body>
      <el-form
        ref="userFormRef"
        label-width="100px"
        :model="userForm"
        :rules="rules"
      >
        <el-form-item label="昵称" prop="nickname">
          <el-input
            placeholder="请输入昵称"
            v-model="userForm.nickname"
            style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="角色" prop="roleIdList">
          <el-checkbox-group v-model="roleIdList">
            <el-checkbox
              v-for="item in userRoleList"
              :key="item.id"
              :label="item.id"
            >
              {{ item.roleName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="update = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getUserList, updateUser, disableUser} from '@/api/user'
import { formatDate } from "@/utils/date";
import {getRoleList} from '@/api/role'
 export default {
  data() {
    return {
      showSearch: true,
      count: 0,
      update: false,
      loading: false,
      queryParams: {
        current: 1,
        size: 10,
      },
      userList: [],
      userForm: {},
      userRoleList: [],
      roleIdList: [],
      rules:{
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        roleIdList: [{ required: true, message: "角色不能为空", trigger: "click" }]
      },
    };
  },
  methods: {
    handleDelete(id){
      if(id == undefined){
        this.$confirm('是否删除选中用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    },
    submitForm(){
      this.userForm.roleIdList = this.roleIdList;
      updateUser(this.userForm).then((data)=>{
        if(data.code == 200){
          this.$message({type: 'success', message: '修改成功'})
          this.update = false;
          this.getList();
        }
      })
    },
    handleQuery(){
      this.getList();
    },
    formattedDate(date) {
      return formatDate(date);
    },
    openModel(data){
      this.update = true;
      this.userForm = data;
      this.roleIdList = data.roleList.map((item)=>item.id);
      getRoleList().then((data)=>{
        if(data.code == 200){
          this.userRoleList = data.data;
        }
      })
    },
    getList(){
      this.loading = true;
      getUserList(this.queryParams).then((data)=>{
        if(data.code == 200){
          this.userList = data.data.recordList;
          this.count = data.data.count;
          this.loading = false;
        }
      })
    },
    handleChangeStatus(data){
      disableUser(data).then((data)=>{
        if(data.code == 200){
          this.$message({type: 'success', message: '操作成功！'})
        }
      })
    },
    changeShowSearch(data){
      this.showSearch = data;
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style>
</style>